package cn.core.sign.rest;

import cn.core.sign.ao.LoginAO;
import cn.core.sign.service.intf.ISignService;
import cn.core.sign.vo.LoginVO;
import cn.core.tool.exception.CoreException;
import cn.core.tool.rest.CoreController;
import cn.core.tool.util.CoreTool;
import cn.core.tool.util.Param;
import cn.core.tool.util.Res;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 登录 REST API
 */
@Log4j2
@RestController
@Scope("prototype")
@RequestMapping("login")
public class LoginController extends CoreController {

  @Resource
  private ISignService signService;

  /**
   * 登录
   * @param param 参数
   * @return 返回
   */
  @PostMapping()
  public Res<LoginVO> login(@RequestBody Param<LoginAO> param) throws Exception {
    Res<LoginVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(signService.login(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

}
